#include <cstdint>
#include <iostream>
#include <istream>
#include <tuple>
#include <vector>
using ll = int64_t;

ll n,x,y,z,ans0;
struct DP{
    ll val;
    ll x,y;
};

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>n;
    std::vector<std::vector<ll>> map(n+1,std::vector<ll>(n+1));
    std::vector<std::vector<DP>> dp(n+1,std::vector<DP>(n+1));
    do {
        std::cin>>x>>y>>z;
        map[x][y]=z;
    }while (x|y|z);
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++){
            if(dp[i][j].val<dp[i][j-1].val+map[i][j]){
                dp[i][j].val = dp[i][j-1].val + map[i][j];
                dp[i][j].x = i;
                dp[i][j].y = j-1;
            }
            if(dp[i][j].val<dp[i-1][j].val+map[i][j]){
                dp[i][j].val = dp[i-1][j].val + map[i][j];
                dp[i][j].x = i-1;
                dp[i][j].y = j;
            }
        }
    }
    ans0=dp[n][n].val;
    map[n][n]=0;
    while(true){
        static ll nx=dp[n][n].x,ny=dp[n][n].y;
        if((nx|ny)==0){
            break;
        }
        map[nx][ny]=0;
        auto[val,x,y] = dp[nx][ny];
        nx=x,ny=y;
    }
    dp.clear();
    dp.resize(n+1,std::vector<DP>(n+1));
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++){
            if(dp[i][j].val<dp[i][j-1].val+map[i][j]){
                dp[i][j].val = dp[i][j-1].val + map[i][j];
                dp[i][j].x = i;
                dp[i][j].y = j-1;
            }
            if(dp[i][j].val<dp[i-1][j].val+map[i][j]){
                dp[i][j].val = dp[i-1][j].val + map[i][j];
                dp[i][j].x = i-1;
                dp[i][j].y = j;
            }
        }
    }
    // std::cout<<dp[n][n].val<<"\n";
    std::cout<<dp[n][n].val+ans0<<"\n";
}